package website;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.anbang.website.service.ProductionsHelper;
import com.anbang.website.service.SystemServiceHelper;
import com.anbang.website.utils.Constant;
import com.anbang.website.vo.Productions;
import com.anbang.website.vo.ResData;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import net.sf.json.JSONObject;

public class Mytest{
	
	@Before
	public void setup() throws IOException{
		System.out.println(this.getClass().getClassLoader().getResource("website.properties"));
		InputStream is = getClass().getClassLoader().getResourceAsStream("website.properties");
		if(null != is){
		Constant.webconfig.clear();
		Constant.webconfig.load(is);
		is.close();
		}
	}

	@Test
	public void testTest(){		
		JSONObject obj = SystemServiceHelper.test();
		System.out.println(obj.toString());
	}
	
	@Test
	public void testProductionsList(){
		String proName="创口贴";
		String proCatalog="21";
		String qtype="3";

		JSONObject obj = ProductionsHelper.prodList(proName, qtype, proCatalog, 1, 10);
		System.out.println(obj.toString());
		
		//使用gson 转化返回的json字符串
		Gson gson = new GsonBuilder().create();
		
		//第一种转换方法，直接转化成对象
		List<Productions> retList = gson.fromJson(obj.getJSONObject("data").getString("productionsList"),
				 new TypeToken<List<Productions>>() {
        }.getType());
		for(Productions p: retList){
			System.out.println(p.getProEnterprise());
		}
		//第二种转换方法，转换成map,list
		ResData resData = gson.fromJson(obj.toString(), ResData.class);
		List<Map<String, Object>> retList1 = (List<Map<String, Object>>)resData.getData().get("productionsList");
		for(Map<String, Object> p: retList1){
			System.out.println(p.get("proName"));
			
		}
		
	}
}
